package cn.kgc.canal;

import cn.kgc.base.BaseInfoProperties;
import cn.kgc.utils.RedisUtil;
import com.alibaba.otter.canal.protocol.CanalEntry;
import com.github.benmanes.caffeine.cache.Cache;
import com.xpand.starter.canal.annotation.CanalEventListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 监听SysParams表中数据改变
 *
 * @author YC
 */
@Component
@CanalEventListener
public class SysParamCanalLister {

    @Resource
    private Cache<String, Integer> refreshResumeCountCache;
    @Resource
    private RedisUtil redisUtil;

    public void listenerInsertOrUpdate(CanalEntry.RowData rowData) {
        // 获得新增或更新之后的数据行
        List<CanalEntry.Column> afterColumnsList = rowData.getAfterColumnsList();
        // 遍历字段
        for (CanalEntry.Column column : afterColumnsList) {
            if ("max_resume_refresh_count".equals(column.getName())) {
                // 获取字段值
                int value = Integer.parseInt(column.getValue());
                // 刷新简历数量缓存
                String key = BaseInfoProperties.REDIS_MAX_RESUME_REFRESH_COUNTS;
                refreshResumeCountCache.put(key, value);
                // 刷新简历数量缓存
                redisUtil.set(key, value);
            }
        }
    }

}
